<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/28 0028
 * Time: 下午 2:48
 */
//头部数据
$BASE = array(
    'referer'   => 'https://music.163.com/',
    'cookie'    => 'os=linux; appver=1.0.0.1026; osver=Ubuntu%2016.10; MUSIC_U=78d411095f4b022667bc8ec49e9a44cca088df057d987f5feaf066d37458e41c4a7d9447977352cf27ea9fee03f6ec4441049cea1c6bb9b6; __remember_me=true',
    'useragent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
);
if($_REQUEST['act'] == 'playlist')
{
    $res = get_playlist_info(776508496);
    echo $res;
}
elseif($_REQUEST['act'] == 'musicData')
{
    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
    $API = array(
        'method' => 'POST',
        'url'    => 'http://music.163.com/api/linux/forward',
        'body'   => array(
            'method' => 'POST',
            'params' => array(
                'ids' => array($id),
                'br'  => 128000,
            ),
            'url' => 'http://music.163.com/api/song/enhance/player/url',
        ),
        'encode' => 'netease_AESECB',
        'decode' => 'netease_url',
    );
    $musicData = curl($API, $BASE);
    echo $musicData;
}
elseif($_REQUEST['act'] == 'musicSearch')
{
    $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
    $SearchAPI=array(
        'method' => 'POST',
        'url'    => 'http://music.163.com/api/linux/forward',
        'body'   => array(
            'method' => 'POST',
            'params' => array(
                's'      => $search,
                'type'   => 1,
                'limit'  => 30,
                'total'  => 'true',
                'offset' => $page,
            ),
            'url' => 'http://music.163.com/api/cloudsearch/pc',
        ),
        'encode' => 'netease_AESECB',
        'format' => 'result#songs',
    );
    echo curl($SearchAPI, $BASE);
}
function get_playlist_info($playlist_id)
{
    $url = "http://music.163.com/api/playlist/detail?id=" . $playlist_id;
    return curl_get($url);
}
//普通请求
function curl_get($url)
{
    $refer = "http://music.163.com/";
    $header[] = "Cookie: " . "appver=1.5.0.75771;";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_REFERER, $refer);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
//加密请求请求
function curl($API, $BASE)
{
    $API = netease_AESECB($API);
    $curl = curl_init();
    if (is_array($API['body'])) {
        $API['body']=http_build_query($API['body']);
    }
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $API['body']);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_TIMEOUT, 20);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_IPRESOLVE, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_URL, $API['url']);
    curl_setopt($curl, CURLOPT_COOKIE, $BASE['cookie']);
    curl_setopt($curl, CURLOPT_REFERER, $BASE['referer']);
    curl_setopt($curl, CURLOPT_USERAGENT, $BASE['useragent']);

    for ($i=0;$i <= 3;$i++) {
        $data=curl_exec($curl);
        $info=curl_getinfo($curl);
        $error=curl_errno($curl);
        $status=$error?curl_error($curl):'';
        if (!$error) {
            break;
        }
    }

    curl_close($curl);
    if ($error) {
        return json_encode(
            array(
                'error'  => $error,
                'info'   => $info,
                'status' => $status,
            )
        );
    }

//    $data = netease_url($data);

    return $data;
}

function netease_AESECB($API)
{
    $KEY='7246674226682325323F5E6544673A51';
    $body=json_encode($API['body']);
    if (function_exists('openssl_encrypt')) {
        $body=openssl_encrypt($body, 'aes-128-ecb', pack('H*', $KEY));
    } else {
        $PAD=16-(strlen($body)%16);
        $body=base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, hex2bin($KEY), $body.str_repeat(chr($PAD), $PAD), MCRYPT_MODE_ECB));
    }
    $body=strtoupper(bin2hex(base64_decode($body)));

    $API['body']=array(
        'eparams'=>$body,
    );
    return $API;
}
function netease_url($result)
{
    $data=json_decode($result, 1);
    if($data['data'][0]['uf'] != null) {
        $url=array(
            'url' => str_replace('http://m8', 'https://m8', $data['data'][0]['uf']['url']),
            'br'  =>$data['data'][0]['uf']['br']/1000,
        );
    }
    else{
        $url=array(
            'url' => str_replace('http://m8', 'https://m8', $data['data'][0]['url']),
            'br'  => $data['data'][0]['br']/1000,
        );
    }
    return json_encode($url);
}